const Router = require('./router.js');
const http = require('http');
const methods = require('methods');
function Application() {
    this._router = new Router();

}

methods.forEach(method => {
    Application.prototype[method] = function (path, ...handles) {
        this._router[method](path, handles);
    }
})
Application.prototype.listen = function (...args) {
    const server = http.createServer((req, res) => {
        function done() {
            res.end(`Cannot ${req.method} ${req.url}`);
        }
        this._router.handle(req, res, done);
    })

    server.listen(...args);
}

Application.prototype.use = function (path, handle) {
    this._router.use(path, handle);
}

Application.prototype.param = function (key, handle) {
    this._router.param(key, handle);
}

module.exports = Application;